package set;

import java.util.*;

public class HashMapDemo {
    public static void main(String[] args) {
        Map<String,String> animal=new HashMap<String, String>();
        System.out.println("请输入三组单词对应的注释,并存放到HashMap中");
        Scanner sc=new Scanner(System.in);
        //添加数据
        for (int i=0;i<3;i++){
            System.out.println("请输入key值(单词):");
            String key=sc.next();
            System.out.println("请输入value值(注释)");
            String value=sc.next();
            animal.put(key,value);
        }
        //打印输出value的值(直接使用迭代器)
        System.out.println("****************************");
        System.out.println("使用迭代器输出所有的value");
        Iterator<String> it=animal.values().iterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
        System.out.println("****************************");
        //打印key和value的值
        //通过entrySet方法
        System.out.println("通过entrySet方法得到key-value:");
        Set<Map.Entry<String, String>> entrySet=animal.entrySet();
        for (Map.Entry<String, String> entry:entrySet){
            System.out.print(entry.getKey()+"-");
            System.out.println(entry.getValue());
        }
        System.out.println();
        System.out.println("****************************");
        //通过单词找到注释并输出
        //使用keySet方法
        System.out.println("请输入要查找的单词:");
        String strSearch=sc.next();
        //取得keySet
        Set<String> keySet=animal.keySet();
        //遍历keySet
        for (String key:keySet){
            if (strSearch.equals(key)){
                System.out.println("找到了!"+"键值对为:"+key+"-"+animal.get(key));
                break;
            }
        }

    }
}
